package cn.itcast.web.controller.cargo;

import cn.itcast.domain.cargo.Contract;
import cn.itcast.domain.cargo.ContractExample;
import cn.itcast.domain.system.Module;
import cn.itcast.domain.system.User;
import cn.itcast.service.cargo.ContractService;
import cn.itcast.web.controller.BaseController;
import com.alibaba.dubbo.config.annotation.Reference;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;

@Controller
@RequestMapping("/cargo/contract")
public class ContractController extends BaseController {

	@Reference
	private ContractService contractService;

	//分页查询
	@RequestMapping("/list")
	public String list(@RequestParam(defaultValue = "1") int page, @RequestParam(defaultValue = "10") int size) {
		//1.创建example
		ContractExample example = new ContractExample();
		//2.创建criteria对象
		ContractExample.Criteria criteria = example.createCriteria();
		//3.向criteria对象中设置查询条件
		criteria.andCompanyIdEqualTo(getLoginCompanyId());

		//根据登录人的类型，添加不同的查询条件
		//i.获取当前登录用户
		User user = getLoginUser();
		//ii.获取当前用户的类型
		Integer degree = user.getDegree();
		//iii.判断
		if (degree == 4) {  //普通员工:查看自己的
            //根据创建者id查询
			criteria.andCreateByEqualTo(user.getId());
		}else if(degree==3) { //管理本部门
			//根据创建者部门id查询
			criteria.andCreateDeptEqualTo(user.getDeptId());
		}else if (degree==2){ //管理所有下属部门和人
			//根据创建者部门id模糊查询
			criteria.andCreateDeptLike(user.getDeptId()+"%");
		}

		//通过example添加排序
		example.setOrderByClause("create_time DESC"); //排序的sql片段
		//4.调用查询
		PageInfo info = contractService.findAll(page, size, example);
		request.setAttribute("page",info);
		return "cargo/contract/contract-list";
	}

	/**
	 * 进入保存页面
	 */
	@RequestMapping("/toAdd")
	public String toAdd() {
		return "cargo/contract/contract-add";
	}

	/**
	 * 进入更新页面
	 */
	@RequestMapping("/toUpdate")
	public String toUpdate(String id) {
		//1.根据id查询
		Contract contract = contractService.findById(id);
		request.setAttribute("contract",contract);
		return "cargo/contract/contract-update";
	}

	@RequestMapping("/edit")
	public String edit(Contract contract) {

		contract.setCompanyId(getLoginCompanyId());
		contract.setCompanyName(getLoginCompanyName());

		if(StringUtils.isEmpty(contract.getId())) {

			//在保存购销合同的时候，添加创建人和创建人所在部门
			//1.获取当前登录用户
			User user = getLoginUser();
			//2.设置创建人和创建人所在部门
			contract.setCreateBy(user.getId()); //创建人id
			contract.setCreateDept(user.getDeptId());//创建者所在部门的id
			//保存
			contractService.save(contract);
		}else{
			//更新
			contractService.update(contract);
		}
		return "redirect:/cargo/contract/list.do";
	}


	/**
	 * 合同提交
	 */

	@RequestMapping("/submit")
	public String submit(String id){
		//根据id查询
		//判断状态是否=0
		//设置状态
		//更新
		Contract contract = new Contract();
		contract.setId(id);
		contract.setState(1);
		contractService.update(contract);
		return "redirect:/cargo/contract/list.do";
	}


	/**
	 * 合同取消
	 */
	@RequestMapping("/cancel")
	public String cancel(String id){
		//根据id查询
		//判断状态是否=0
		//设置状态
		//更新
		Contract contract = new Contract();
		contract.setId(id);
		contract.setState(0);
		contractService.update(contract);
		return "redirect:/cargo/contract/list.do";
	}

}
